var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true; 
var txtname1 = '提现';
var txtname2 = '收支明细';
Page({
  data: {
    util: util,
    tc: 0,

    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
    version: util.version,
    token: '',
    uid: 0,
    coin:0.00,
    userinfo:{},

    actionSheetHidden: true,
    actionSheetItems: [
      { bindtap: 'Menu3', txt: txtname2, link: 'mingxi/mingxi' },
      { bindtap: 'Menu1', txt: txtname1, link: 'tixian/tixian' }
      // { bindtap: 'Menu2', txt: '充值', link: 'chongzhi/chongzhi' },
    ],

    isShow: false,

    user_jifen: 0,

    hasKanGoods: false, //是否有砍价活动的商品

    colorSet: '',

    commenItem_w: 110
  },
  onLoad: function(data){
    this.getLoginInfo();

    var that = this;
    var titlename = data.title ? data.title : '我';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    var tc = app.get_tc();//简体繁体转换
    if(tc == 1){
      txtname1 = '提現';
      txtname2 = '收支明細';
    }
    that.setData({
      actionSheetItems: [
        { bindtap: 'Menu3', txt: txtname2, link: 'mingxi/mingxi' },
        { bindtap: 'Menu1', txt: txtname1, link: 'tixian/tixian' }
        // { bindtap: 'Menu2', txt: '充值', link: 'chongzhi/chongzhi' },
      ],
      tc: tc
    })

    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          commenItem_w: res.windowWidth / 3
        });
      }
    });

    this.getGoodsListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });
  },
  //设置titlebar 样式
  resetNavColor: function () {
    // var bg_color = '#f0f0f0';
    // var fg_color = '#000000';
    // if (this.data.colorSet) {
    //   bg_color = this.data.colorSet.titlebg;
    //   fg_color = this.data.colorSet.titlefont;
    // }
    // wx.setNavigationBarColor({
    //   frontColor: fg_color,
    //   backgroundColor: bg_color
    // })
  },


  getJifen: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getusersource",
        "uid": that.data.uid
      },
      success: function (responseData) {
        that.setData({
          user_jifen: responseData.data.source
        })
      }
    });
  },

  getLoginInfo: function(){
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token
      });
      that.getJifen();

      util.api({
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function (responseData) {
          console.log(responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

      var params = {
        "uid": res.uid,
        "publickey": util.publiccode,
        "ac": "coininfo",
        "privatekey": utilMd5.hexMD5(res.uid + 'coininfo' + res.token)
      };
      console.log("余额参数：uid=" + res.uid + "; token = " + res.token, params);
      util.api({
        method: "post",
        data: params,
        success: function (responseData) {
          console.log("余额返回：", responseData);
          that.setData({
            coin: (responseData.data.coin || 0) / 100
          });
        }
      });

      util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "wxcheck"
        },
        success: function (responseData) {
          that.setData({
            isShow: !responseData.data.isShow
          });
        }
      });
    });
  },

  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },

  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function (e) {//提现
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu2: function (e) {//充值
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },
  bindMenu3: function (e) {//收支明细
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.goAtherPage(e);
  },

  goAtherPage: function(e){
    if (wxAuthFail) {
      app.reAuthorize(this.reGetUserInfo);
    } else {
      var url = e.currentTarget.dataset.link;
      if (url.indexOf("my-youhuiquan/my-youhuiquan") > 0){
        wx.showModal({
          title: '提示',
          content: '即将上线',
          showCancel: false
        })
      } else {
        wx.navigateTo({ url: url })
      }
    }
  },
  goAtherPageRedirect: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },

  goAther_miniProgram: function () {
    // wx.showToast({
    //   title: '到艺天下小程序',
    // })
    wx.navigateToMiniProgram({
      appId: 'wx9abd62983ac25aa0',
      path: 'pages/index/index',
      extarData: {
        open: 'happy'
      },
      envVersion: 'release',
      success(res) {
        wx.showToast({
          title: '打开成功',
        })
      },
      fail(res) {
        wx.showToast({
          title: '打开失败' + JSON.stringify(res),
        })
      }
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      if (path.indexOf('navto:') == 0) {
        var navPath = path.slice(6);
        wx.navigateTo({
          url: navPath,
        })
      } else {
        wx.redirectTo({ url: path });
      }
    }
  },

  //切换语言
  changeLanguage: function(){
    var tc = this.data.tc == 1 ? 0 : 1;
    app.set_tc(tc);
    this.setData({
      tc: app.get_tc()
    });
  },

  showEWM: function (e) {
    var shareImgUrl = e.currentTarget.dataset.imgsrc;
    console.log("朋友圈图片链接：" + shareImgUrl);
    wx.previewImage({
      current: shareImgUrl,
      urls: [shareImgUrl]
    })
  },

  getGoodsListData: function () {
    var that = this;
    this.setData({
      hasLoadding: true
    });
    var params = {
      publickey: util.publiccode,
      ac: "topiclist",
      topictype: '4',
      orgid: util.dataUid,
      postuid: '',
      kan: '1',
      page: 1,
      num: 2
    };
    util.api({
      data: params,
      success: function (responseData) {
        var resData = responseData.data;
        console.log("砍价活动商品数据：", resData);
        if (!resData.msg && resData.length > 0) {
            that.setData({
              hasKanGoods: true
            });
        }
      }
    });
  },
})